package com.banch.cloud.hystrix.controller;

import com.banch.cloud.hystrix.feign.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * order controller
 *
 * @author banch
 * @version 1.0
 * @date 2020/7/10
 */
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod", commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
@RequestMapping(value = "/consumer/order/hystrix")
@Controller
public class OrderHystrixController {

    /**
     *
     */
    @Resource
    private PaymentHystrixService paymentHystrixService;


    /**
     * description
     *
     * @param
     * @return
     * @author banch
     * @date 2020/7/10
     * @throw
     **/
    @ResponseBody
    @GetMapping(value = "/ok/{id}")
    String paymentOK(@PathVariable(value = "id") Integer id) {
        return paymentHystrixService.paymentOK(id);
    }


    /**
     * description
     *
     * @param
     * @return
     * @author banch
     * @date 2020/7/10
     * @throw
     **/
    /* @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",
             commandProperties = {
                     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")  //3秒钟以内就是正常的业务逻辑，消费端配置三秒超时
             })*/
    @HystrixCommand
    @ResponseBody
    @GetMapping(value = "/timeout/{id}")
    String paymentTimeout(@PathVariable(value = "id") Integer id) {
        return paymentHystrixService.paymentTimeout(id);
    }

    /**
     * 降级方法
     *
     * @param
     * @return
     * @author banch
     * @date 2020/7/10
     * @throw
     **/
    public String paymentTimeOutFallbackMethod(@PathVariable(value = "id") Integer id) {
        return "paymentTimeOutFallbackMethod()  我是消费者80，对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬＿┬),id:" + id;
    }

    /**
     * 默认降级方法
     *
     * @param
     * @return
     * @author banch
     * @date 2020/7/10
     * @throw
     **/
    public String payment_Global_FallbackMethod() {
        return "payment_Global_FallbackMethod()  我是消费者80，系统繁忙请10秒钟后再试,id:";
    }

}